package com.base.controller;

import com.base.po.czb.GasOrderH5read;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.CZB.GasOrderH5readService;
import com.base.service.SubscribersService;
import com.base.service.UsersService;
import com.base.utils.Page;
import com.base.utils.Produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class H5GasOrderController {
    @Autowired
    SubscribersService subscribersService;
    @Autowired
    UsersService usersService;
    @Autowired
    GasOrderH5readService gasOrderH5readService;

    @RequestMapping("/content_gasorderH5")
    public ModelAndView index(HttpServletRequest request, Integer currentPageNo,
                              String mobile, String czborderid, String parentid, String gasid, String paystatus, String createTime1, String createTime2,String dateRange) throws Exception {
        Map<String, Object> map = new HashMap<>(); // 返回数据对象
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        if (adminUserId == null) {
            return new ModelAndView("jsp/index_H5GasOrder", map);
        }

        if (currentPageNo == null) {
            currentPageNo = 1;
        }
        if (paystatus == null || paystatus.equals("") || paystatus.equals("0")) {
            paystatus = null;
        }
        String username="";

        if (parentid == null || parentid.equals("")) {
            parentid = null;
        }else{
            try{
                Users agent=usersService.selectByUsername(parentid);
                username=agent.getUsername();
                if (agent==null){
                    map.put("msg", "商户不存在");
                    return new ModelAndView("jsp/content_gasorder", map);
                }
                parentid=agent.getId();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        Subscribers subscribers;
        String userId = null;

        if (mobile == null || mobile.equals("")) {
            mobile = null;
        } else {
            if (!Produce.isMobile(mobile)) {
                map.put("msg", "请输入正确的手机号");
                return new ModelAndView("jsp/content_gasorder", map);
            }
            subscribers = subscribersService.selectBySubscribers(mobile, adminUserId);
            if (subscribers == null) {
                userId = null;
            } else {
                userId = subscribers.getId();
            }
        }

        if (czborderid == null || czborderid.equals("")) {
            czborderid = null;
        }

        if (gasid == null || gasid.equals("")) {
            gasid = null;
        }

        if (paystatus == null || paystatus.equals("")) {
            paystatus = null;
        }
        //截取时间
        if (dateRange!=null && !dateRange.equals("")){
            String[] str = dateRange.split(" - ");
            createTime1=str[0];
            createTime2=str[1];
        }

        // 设置分页 获取内容
        Page page = new Page(); // 实例化分页
        if (currentPageNo != null) page.setCurrentPageNo(currentPageNo); // 设置当前页

        page.setCurrentPage((currentPageNo - 1) * page.getPageSize());

        Integer gasOrderCount = gasOrderH5readService.countAllGasOrder(parentid, czborderid, mobile, gasid, paystatus, createTime1, createTime2);

        List<GasOrderH5read> gasOrders = gasOrderH5readService.selectAllGasOrder(parentid, czborderid,mobile, gasid, paystatus, createTime1, createTime2, page);
        BigDecimal shopf=new BigDecimal(0);
        shopf=gasOrderH5readService.countAllOrderMoney(parentid, czborderid,mobile, gasid, paystatus, createTime1, createTime2);
        for (GasOrderH5read item : gasOrders) {
            if (item.getParentid()!=null && !item.getParentid().equals("")){
                String parentid1=item.getParentid();
                Users users=usersService.selectByPrimaryKey(parentid1);
                item.setParentName(users.getUsername());
            }
        }
        /**
         *  @Date: 2020/1/17 14:43
         *  @Description:
         *  获取所有的油站数据
         */
        page.setRecordTotal(gasOrderCount); // 设置总条数
        map.put("msg", "查询成功");
        map.put("shopf",shopf);
        map.put("mobile", mobile);
        map.put("czborderid", czborderid);
        map.put("payParentId", username);
        map.put("test10", dateRange);
        map.put("paystatus", paystatus);
        map.put("gasOrderList", gasOrders); // 提现集合
        map.put("pages", page); // 分页
        return new ModelAndView("jsp/index_H5GasOrder", map);
    }

//    @RequestMapping("/gasOrderPush")
//    public @ResponseBody
//    NetState CZBpush(HttpServletRequest request,String id,String mobile) throws Exception {
//        NetState netState = new NetState();
//        int orderid=Integer.parseInt(id);
//       GasOrderH5read gasOrderH5read= gasOrderH5readService.selectByPrimaryKey(orderid);
//       if (gasOrderH5read==null){
//           netState.setErrorCode(NetState.FEAILED);
//           netState.setData("查询无此订单");
//           return netState;
//       }
//        gasOrderH5read.get
//        netState.setErrorCode(NetState.SUCCESS_CONN);
//        return netState;
//    }
}
